import Vue from 'vue'
import VueRouter from 'vue-router'
import api from "../http/apis/apis"
Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: "/system",
  }, {
    path: "/login",
    name: "login",
    component: () => import("../views/login/login.vue"),
  }, {
    path: "/system",
    name: "system",
    component: () => import("../views/system/system.vue"),
    //系统路由独享守卫
    beforeEnter: async (to, from, next) => {
      const { data } = await api.adminApi.isLogin();
      console.log('路由守卫', data);
      if (!data.status && data.type == 401) {
        alert('你还未登录，请先登录');
        next('/login');
      } else {
        next();
      }
    },
    children: [
      //管理员路由
      {
        path: "admin",
        name: "admin",
        component: () => import("../views/system/admin/admin.vue"),
        children: [{
          path: "addadmin",
          name: "addadmin",
          component: () => import("../components/admin/AddAdmin.vue")
        },
        {
          path: "adminlist",
          name: "adminlist",
          component: () => import("../components/admin/AdminList.vue")
        },
        {
          path: "updataadmin/:_id",
          name: "updataadmin",
          component: () => import("../components/admin/UpDataAdmin.vue")
        },
        ]
      },
      //用户路由
      {
        path: "user",
        name: "user",
        component: () => import("../views/system/user/user.vue"),
        children: [{
          path: "adduser",
          name: "adduser",
          component: () => import("../components/user/AddUser.vue")
        }, {
          path: "userlist",
          name: "userlist",
          component: () => import("../components/user/UserList.vue")
        }]
      },
      {
        path: "car",
        name: "car",
        component: () => import("../views/system/car/car.vue"),
        children: [{
          path: "addCar",
          name: "addCar",
          component: () => import("../components/car/AddCar.vue")
        }, {
          path: "carList",
          name: "carList",
          component: () => import("../components/car/CarList.vue")
        }, {
          path: "upDataCat",
          name: "upDataCat",
          component: () => import("../components/car/UpDataCat.vue")
        }
        ]
      },
      {
        path: "order",
        name: "order",
        component: () => import("../views/system/order/order.vue"),
        children: [{
          path: "addorder",
          name: "addorder",
          component: () => import("../components/order/AddOrder.vue")
        },
        {
          path: "orderlist",
          name: "orderlist",
          component: () => import("../components/order/OrderList.vue")
        }]
      },
      {
        path: "carcomemnts",
        name: "carcomemnts",
        component: () => import("../views/system/customerComments/customerComments"),
        children: [{
          path: "addcarcoments",
          name: "addcarcoments",
          component: () => import("../components/carComments/AddComments.vue")
        }
       ]
      },
      {
        path: "customercomme",
        name: "customercomme",
        component: () => import("../views/system/carComments/carComments.vue"),
        children: [{
          path: "addcarcoments",
          name: "addcarcoments",
          component: () => import("../components/customerComme/AddCustomer.vue")
        }
       ]
      },
      {
        path: "lineargraph",
        name: "lineargraph",
        component: () => import("../views/system/lineargraph/lineargraph.vue"),
        children: [{
          path: "addlineargraph",
          name: "addlineargraph",
          component: () => import("../components/lineargraph/linearGraph.vue")
        }
       ]
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

export default router
